class Game {
    constructor() {
        //初始化
        this.sky = new Sky()
        this.land = new Land(-100)
        this.bird = new Bird()

        //柱子对生成器
        this.pipePareProducer = new PipePareProducer(-100)

        this.timer = null;
        this.tick = 16; //移动时间间隔，毫秒

        this.gameOver = false;
    }

    start() {
        if (this.timer) return

        if (this.gameOver) {
            //重新开始游戏
            window.location.reload()
        }

        //煽动翅膀
        this.bird.startSwing()
        //持续生产柱子对 
        this.pipePareProducer.startProducer()

        const duration = this.tick / 1000
        this.timer = setInterval(() => {
            this.sky.move(duration)
            this.land.move(duration)
            this.bird.move(duration)
            this.pipePareProducer.pairs.forEach(pair => {
                pair.move(duration)
            })

            if (this.isGameOver()) {
                this.stop()
                this.gameOver = true
            }


        }, this.tick)
    }


    stop() {
        clearInterval(this.timer);
        this.timer = null;
        this.bird.stopSwing()
        this.pipePareProducer.stopProduce()
    }

    /**
     * 判断两个矩形是否相撞
     * @param {*} rec1 
     * @param {*} rec2 
     */
    isHit(rec1, rec2) {
        var x1 = rec1.left + (rec1.width / 2)
        var y1 = rec1.top + (rec1.height / 2)
        var x2 = rec2.left + (rec2.width / 2)
        var y2 = rec2.top + (rec2.height / 2)
        var disX = Math.abs(x1 - x2)   //中心点横向距离
        var disY = Math.abs(y1 - y2)   //中心点横向距离

        if (disX < (rec1.width + rec2.width) / 2 &&
            disY < (rec1.height + rec2.height) / 2
        ) {
            return true;
        }
        return false;
    }

    /**
     * 判断游戏是否结束
     * 1.小鸟掉地上
     * 2.小鸟和管道碰撞到
     */
    isGameOver() {
        if (this.bird.top >= this.bird.maxY) {
            return true
        }

        return this.pipePareProducer.pairs.some(pair => {
            //看柱子对pairs是否跟bird进行了碰撞
            return this.isHit(this.bird, pair.upPipe) || this.isHit(this.bird, pair.downPipe)
        })
    }


    //键盘事件
    regEvent() {
        window.onkeydown = (e) => {
            //判断游戏是否开启，若开启了则暂停
            if (e.key === "Enter") {
                if (this.timer) {
                    this.stop();
                }
                else {
                    this.start();
                }
            }
            else if (e.key === " ") {
                this.bird.jump();
            }
        }
    }
}

var g = new Game();
g.regEvent()

// g.start()


/**
 * 我的笔记：
  1.使用some代替for循环   代码87行
 
  2.计算两个矩形是否相撞：计算中心点的距离
   0 ───────────────────────────────────────────────> X
           50       100       150
           |--------|--------|
           ↑        ↑        ↑
         left    center    right

         center = 50 + 100 / 2 = 100（矩形的中心点）

    3.事件的绑定，this指向事件元素本身
    window.onkeydown = function(e){
            console.log(this);    //会指向window
        }

    4.需要加上isGameOver判断游戏是否结束,游戏结束需要在stop（）补充，关闭翅膀煽动以及生成柱子对
 */

